﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SLMapViewer.Web
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Get the UI culture
            System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CurrentUICulture;
            
            // Mapfile to use
            string mapfile = System.Configuration.ConfigurationManager.AppSettings["mapfile"];
            Session["mapfile"] = mapfile;
            Session["MAPWIDTH"] = System.Configuration.ConfigurationManager.AppSettings["mapwidth"];
            Session["MAPHEIGHT"] = System.Configuration.ConfigurationManager.AppSettings["mapheight"];

            //Default the active layer to the first layer in the mapfile
            Session["ACTIVELAYER_IDX"] = 0;

            // Textbox to be used to receive responses
            // from the silverlight control. 
            // This control MUST EXIST IN THE ASPX PAGE
            string sTargetControlID = "txtBox1";
            
            // Textbox to be used to send map request to
            // the silverlight control. 
            // This control MUST EXIST IN THE ASPX PAGE
            string sRequestControlID = "TextBox1";
            
            // Application path
            string appPath = Request.ApplicationPath;

            // Make sure the base url sent to the silverlight control is correct.
            // Otherwise you get the "the remote server returned an error: not found"
            // when trying to do the async request to xml.aspx

            if (appPath == "/") { appPath = string.Empty; }
            string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority) + appPath;

            // Whether the legend will appear inside or next to the map window
            // 1 will make the legend appear inside the map
            string strInternalControls = "0";

            // Display debug window or not
            string sDebug = "0";
            if (strInternalControls == "1")
            {

                Silverlight1.Width = new Unit(Convert.ToDouble(Session["MAPWIDTH"].ToString()) + 20);
                Silverlight1.Height = new Unit(Convert.ToDouble(Session["MAPHEIGHT"].ToString()));
            }
            else
            {
                Silverlight1.Width = new Unit(Convert.ToDouble(Session["MAPWIDTH"].ToString()) + 300);
                Silverlight1.Height = new Unit(Convert.ToDouble(Session["MAPHEIGHT"].ToString()));
            }
            //;
            string sParamString = String.Empty;
            sParamString = sParamString + "MapServiceUrl=" + baseUrl + "/MapServer/xml.aspx,";
            sParamString = sParamString + "Width=" + Session["MAPWIDTH"].ToString() + ",";
            sParamString = sParamString + "Height=" + Session["MAPHEIGHT"].ToString() + ",";
            sParamString = sParamString + "InternalControls=" + strInternalControls + ",";
            sParamString = sParamString + "DebugOn=" + sDebug + ",";
            sParamString = sParamString + "Culture=" + ci.Name + ",";
            sParamString = sParamString + "TargetControlID=" + sTargetControlID + ",";
            sParamString = sParamString + "RequestControlID=" + sRequestControlID + ",";
            sParamString = sParamString + "SessionID=" + string.Empty;
            Silverlight1.InitParameters = sParamString;
            //QueryControl1.InitParameters = "ControlId=QueryControl";
        }

        protected void btnMapReload_OnClick(object sender, EventArgs e)
        {
            this.upnlMap.Update();
        }
    }
}
